package com.zcsy.manage.service.base.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcsy.commons.constants.Constants;
import com.zcsy.manage.model.base.CellUnit;
import com.zcsy.manage.persistence.base.CellUintDao;
import com.zcsy.manage.service.base.ICellHouseService;
import com.zcsy.manage.service.base.ICellUnitService;
import com.zcsy.manage.service.system.impl.SystemComService;


/**
 * @Title: CellUnitService.java
 * @Package： com.zcsy.manage.service.base.impl
 * @Description:
 * @author zhangsh zsh3384027@gmail.com
 * @Copyright: 2016 武汉恒数利通技术有限公司 All rights reserved.
 * @date 2016年4月9日
 * @version 1.0
 * @see JDK1.8.0_45
 * @since
 */
@Service
public class CellUnitServiceImpl extends SystemComService implements ICellUnitService {

    @Autowired
    private CellUintDao cellUintDao;

    @Autowired
    private ICellHouseService cellHouseService;
	
    @Override
    public int insert(CellUnit cellUnitInfo) {
        return cellUintDao.insert(cellUnitInfo);
    }

    @Override
    public int update(CellUnit cellUnitInfo) {
    	CellUnit oldUnit = cellUintDao.selectByPrimaryKey(cellUnitInfo.getId());
    	
    	//单元名称发生变化时需要同步对应单元id下房屋表字段下的名称
    	if (!(oldUnit.getName()).equals(cellUnitInfo.getName())) {
    		cellHouseService.updateById(oldUnit.getId(), cellUnitInfo.getName(), Constants.CELL_TYPE);
		}
        
    	oldUnit.setName(cellUnitInfo.getName());
    	oldUnit.setRegionId(cellUnitInfo.getRegionId());
    	oldUnit.setBuildingId(cellUnitInfo.getBuildingId());
    	oldUnit.setRegionName(cellUnitInfo.getRegionName());
    	oldUnit.setBuildingName(cellUnitInfo.getBuildingName());
    	oldUnit.setMemo(cellUnitInfo.getMemo());
    	oldUnit.setSort(cellUnitInfo.getSort());
    	oldUnit.setUserAccount(cellUnitInfo.getUserAccount());
    	oldUnit.setUpdateTime(cellUnitInfo.getUpdateTime());
    	
        return cellUintDao.update(oldUnit);
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return cellUintDao.deleteByPrimaryKey(id);
    }

    @Override
    public CellUnit selectByPrimaryKey(String id) {
        return cellUintDao.selectByPrimaryKey(id);
    }

    @Override
    public List<CellUnit> getCellUnitByCellId(String cellId) {
        return cellUintDao.getCellUnitByCellId(cellId);
    }

    @Override
    public List<CellUnit> selectByKeyword(CellUnit cellUnitInfo) {
        return cellUintDao.selectByKeyword(cellUnitInfo);
    }

    @Override
    public List<CellUnit> getCellUnitByBuildingId(String buildingId) {
        return cellUintDao.getCellUnitByBuildingId(buildingId);
    }

    @Override
    public int updateById(String id, String name, int type) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("name", name);
        map.put("type", type);
        return cellUintDao.updateById(map);
    }

    /*
     * (non-Javadoc)
     * @see
     * com.zcsy.manage.service.base.ICellUnitService#selectForImport(com.zcsy.manage.model.base
     * .CellUnit)
     */
    @Override
    public List<CellUnit> selectForImport(CellUnit record) {
        return cellUintDao.selectForImport(record);
    }

	@Override
	public int updateSort(CellUnit record) {
		return cellUintDao.updateSort(record);
	}

    @Override
    public boolean isNameExists(String unitId, String buildingid, String name) {
        return cellUintDao.isNameExists(unitId, buildingid, name);
    }

}
